<?php
    include ('functions.php');
    session_start();
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>THE SHOP :: ordering</title><link rel="stylesheet" type="text/css" href="style.css"/></head><body><div id="logo"><a href="index.php"><img src="img/logo.PNG" alt="THE SHOP"/></a></div>
<div id="main">
 
 <table width="100%" cellspacing="0" cellpadding="0">
    <tr><td id="mainpart"> <div id="menutable">
    <table width="95%" border="0">
      <tr>
        <td><p id="menu"><span><a href="index.php">Main page</a></span> :: <a href="products.php">Product list</a> :: <a href="faq.php">FAQ</a> :: <a href="info.php">About</a> :: <a href="cart.php" id="selected">Shopping cart</a></p></td>
        <td><form action="search.php" method="post" id="searchfield">
          <input name="search" type="text" value="Search now!" size="20" maxlength="100" onClick="this.value=''" />
          <input name="submit" type="submit" value="Search" />
        </form></td>
      </tr>
    </table>
  </div>
            <table id="layouttable"><tr><td>
        <!-- MAIN PART -->
        <?php

        #Checking if all fields have been entered
        $errorArray = array();
        $errorArraySyntax = array();
        if (!$_POST["fname"]) {
            $errorArray[] = "First Name";
            }
        else {
            if (!preg_match("^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$^", $_POST["fname"])) {
                $errorArraySyntax[] = "First Name";
                }
        }
        if (!$_POST["sname"]) {
            $errorArray[] = "Second Name";
            }
            else {
                if (!preg_match("^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$^", $_POST["sname"])) {
                    $errorArraySyntax[] = "Second Name";
                }
            }
        if (!$_POST["gender"]) {
            $errorArray[] = "Gender";
            }
//        if (!$_POST["country"]) {
//            $errorArray[] = "Country";
//            }
        if (!$_POST["postalcode"]) {
            $errorArray[] = "Postal Code";
            }
            else {
                if (!preg_match("^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$^", $_POST["postalcode"])) {
                    $errorArraySyntax[] = "Postal Code";
                }
            }
        if (!$_POST["town"]) {
            $errorArray[] = "Town/City";
            }
            else {
                if (!alpha_numeric($_POST["town"], 2)) {
                    $errorArraySyntax[] = "Town/City";
                    }
            }
        if (!$_POST["address"]) {
            $errorArray[] = "Address";
            }

//        #Checking syntax
//        if (!alpha_numeric($_POST["fname"])) {
//            $errorArraySyntax[] = "First Name";
//            }
//        if (!alpha_numeric($_POST["sname"])) {
//            $errorArraySyntax[] = "Second Name";
//            }
////        if (!alpha_numeric($_POST["country"])) {
////            $errorArraySyntax[] = "Country";
////            }
//        if (!alpha_numeric($_POST["postalcode"])) {
//            $errorArraySyntax[] = "Postal Code";
//            }
//        if (!alpha_numeric($_POST["town"], 2)) {
//            $errorArraySyntax[] = "Town/City";
//            }
//        if (!alpha_numeric($_POST["address"], 3)) {
//            $errorArraySyntax[] = "Address";
//            }

        $theLenght=count($errorArray);
        $theLenghtSyntax=count($errorArraySyntax);
        if ($theLenght or $theLenghtSyntax) {
            if ($theLenghtSyntax == 1) {
                 print "You have syntax errors in a field: ";
                 print $errorArraySyntax[0];
                 }

            else if ($theLenghtSyntax > 1) {
                print "You have syntax errors in field: ";
                print $errorArraySyntax[0];
                for ($i = 1; $i < $theLenghtSyntax; $i++) {
                    print ", ";
                    print $errorArraySyntax[$i];
                }
            }

            if ($theLenght == 1) {
                if ($theLenghtSyntax) {
                    print "<br>";
                }
                print "You have not entered a field: ";
                print $errorArray[0];
                 }

            else if ($theLenght > 1) {
                if ($theLenghtSyntax) {
                    print "<br>";
                }
                print "You have not entered fields: ";
                print $errorArray[0];
                for ($i = 1; $i < $theLenght; $i++) {
                    print ", ";
                    print $errorArray[$i];
                }
            }
            $_SESSION["errorfname"] = $_POST["fname"];
            $_SESSION["errorsname"] = $_POST["sname"];
            $_SESSION["errorgender"] = $_POST["gender"];
            $_SESSION["errorpostalcode"] = $_POST["postalcode"];
            $_SESSION["errortown"] = $_POST["town"];
            $_SESSION["erroraddress"] = $_POST["address"];
            $_SESSION["errorcountry"] = $_POST["country"];

            ?>
        <form action="checkout.php" method="post">
          <input name="goback" type="submit" value="BACK"/>
        </form>
        <?
        }



        else {
            #everything is fine with checks
            $_SESSION["fname"] = $_POST["fname"];
            $_SESSION["sname"] = $_POST["sname"];
            $_SESSION["gender"] = $_POST["gender"];
            $_SESSION["postalcode"] = $_POST["postalcode"];
            $_SESSION["town"] = $_POST["town"];
            $_SESSION["address"] = $_POST["address"];
            $_SESSION["country"] = $_POST["country"];
        ?>

        <table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td id="checkoutleft">First Name:</td>
    <td><? echo $_POST["fname"]; ?></td>
  </tr>
  <tr>
    <td id="checkoutleft">Second Name:</td>
    <td><? echo $_POST["sname"]; ?></td>
  </tr>
  <tr>
    <td id="checkoutleft">Gender:</td>
    <td><? echo $_POST["gender"]; ?></td>
  </tr>
  <tr>
    <td id="checkoutleft">Country:</td>
    <td><? echo $_POST["country"]; ?></td>
  </tr>
  <tr>
    <td id="checkoutleft">Postal Code:</td>
    <td><? echo $_POST["postalcode"]; ?></td>
  </tr>
  <tr>
    <td id="checkoutleft">Town:</td>
    <td><? echo $_POST["town"]; ?></td>
  </tr>
  <tr>
    <td id="checkoutleft">Address:</td>
    <td><? echo $_POST["address"]; ?></td>
  </tr>
</table>

        <br>
        Do you confirm your purchase?
          <table width="10%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><form action="purchase.php" method="post">
          <input name="answer" type="submit" value="yes"/>
        </form></td>
    <td><form action="cart.php" method="post">
          <input name="answer" type="submit" value="no" />
        </form></td>
  </tr>
</table>
        
        
        <?
        $array1 = selectAll();
        checkOut($array1);
        }
        ?>
        <!-- END OF MAIN PART -->
        </td></tr></table>
        </td>
    </tr>
  </table>
        <? putFooter();
        ?>
</div>

</body></html>